(function() {
  $(function() {
    var echo_close, form, i_username, last_login, update_text;
    form = $('form.ui.form');
    i_username = form.find('input[name="username"]');
    last_login = Cookies.get('last.login.username');
    i_username.val(last_login);
    if (i_username.val() === "") {
      i_username.focus();
    } else {
      form.find('input[name="password"]').focus();
    }
    form.form({
      fields: {
        username: {
          rules: [
            {
              type: 'empty',
              prompt: '必填'
            }
          ]
        },
        password: {
          rules: [
            {
              type: 'empty',
              prompt: '必填'
            }
          ]
        }
      }
    });
    form.submit(function() {
      Cookies.set('last.login.username', form.find('input[name="username"]').val(), {
        expires: 365,
        path: '/'
      });
      return true;
    });
    echo_close = $('#echo_close');
    if (echo_close) {
      echo_close.click(function() {
        return $(this).parent("#echo_message").addClass('hidden');
      });
    }
    if (form.find('.submit.button').data('locked')) {
      update_text = function() {
        var button, minutes, seconds, wait_time;
        button = form.find('.submit.button');
        wait_time = button.data('wait-time');
        minutes = Math.floor(wait_time / 60);
        seconds = wait_time % 60;
        button.text(("将在 " + minutes + " 分 ") + ("" + seconds + " 秒后解锁，请等待..."));
        button.data('wait-time', wait_time - 1);
        if (parseInt(wait_time) <= 0) {
          return window.location = '/login';
        }
      };
      return setInterval(update_text, 1000);
    }
  });

}).call(this);
